import { defineStore } from 'pinia'
import axios from 'axios'

//选项式
// export const useTalkStore = defineStore('talk', {
//     actions: { //方法(修改数据)
//         async getTalk() {
//             let { data: title } = await axios.get('https://api.kekc.cn/api/yiyan')
//             this.talkList.push({ id: 1, 'name': title })
//         }
//     },
//     state() { //真正存储数据的地方
//         return {
//             // talkList: [
//             //     { 'id': 1, 'name': 'zhangsan' },
//             //     { 'id': 1, 'name': 'zhangsan' }
//             // ]
//             talkList: JSON.parse(localStorage.getItem('talkList') as string) || []
//         }
//     }
// })

//组合式
import { ref, reactive } from 'vue'
export const useTalkStore = defineStore('talk', () => {
    const talkList = reactive( //state
        JSON.parse(localStorage.getItem('talkList') as string) || []
    )
    async function getTalk() { //actions方法(修改数据)
        let { data: title } = await axios.get('https://api.kekc.cn/api/yiyan')
        talkList.push({ id: 1, 'name': title })
    }
    return { talkList, getTalk }
})